package com.zhhtech.zhh.ui.activity.main

import android.content.Intent
import android.net.Uri
import android.view.View
import androidx.core.view.isVisible
import com.zhhtech.zhh.api.api
import com.zhhtech.zhh.api.core.FirmException
import com.zhhtech.zhh.api.core.launchUI
import com.zhhtech.zhh.api.core.toData
import com.zhhtech.zhh.bean.CaseConfig
import com.zhhtech.zhh.databinding.ActivityCustomerServiceBinding
import com.zhhtech.zhh.ui.api.ApiObserver
import com.zhhtech.zhh.ui.base.BaseActivity
import com.zhhtech.zhh.ui.ext.clipboard


class CustomerServiceActivity : BaseActivity<ActivityCustomerServiceBinding>() {
    private var mCaseConfig: CaseConfig? = null
    override fun initView() {
        bindViewClick(vb.tvWecomCopy, vb.tv400Call, vb.tv1v1Call)
        launchUI({
            mCaseConfig = api.indexConfig().toData() ?: throw FirmException("客服信息获取失败")
            vb.tvWecom.text = mCaseConfig?.weCom ?: ""
            vb.llWecom.isVisible = vb.tvWecom.length() > 0

            vb.tv400.text = mCaseConfig?.phone ?: ""
            vb.ll400.isVisible = vb.tv400.length() > 0

            vb.tv1v1.text = mCaseConfig?.contactNumber ?: ""
            vb.ll1v1.isVisible = vb.tv1v1.length() > 0
        }, ApiObserver(mThis, showLoadingDialog = false))
    }

    override fun onClick(v: View?) {
        mCaseConfig?.let {
            when (v) {
                vb.tvWecomCopy -> mCaseConfig?.weCom.clipboard()
                vb.tv400Call -> startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:${mCaseConfig?.phone}")))
                vb.tv1v1Call -> startActivity(Intent(Intent.ACTION_DIAL, Uri.parse("tel:${mCaseConfig?.contactNumber}")))
            }
        }
    }
}